#!/usr/bin/env python
# coding: utf-8

# In[ ]:


import pandas as pd
import datetime
import os
## 日志转dataframe
def log_to_df_new(data):
    data = data.drop(0)
    data = data.rename(columns={'ApplyLoan_New_apply_date':'apply_date'})
    df_new = data[['id','apply_date','ApplyLoan_New_api_code','ApplyLoan_New_flag_id','ApplyLoan_New_flag_cell']]
#    df_new.head(1)
    
    apply_date_list_all = []
    for i in range(len(df_new)):
        apply_date_list = str(df_new['apply_date'].iloc[i]).split('，')
        apply_date_list_all.append(apply_date_list)
    df_new['apply_date'] = apply_date_list_all
    
    api_code_list_all = []
    for i in range(len(df_new)):
        api_code_list = str(df_new['ApplyLoan_New_api_code'].iloc[i]).split('，')
        api_code_list_all.append(api_code_list)
    df_new['api_code'] = api_code_list_all
    
    flag_id_list_all = []
    for i in range(len(df_new)):
        flag_id_list = str(df_new['ApplyLoan_New_flag_id'].iloc[i]).split('，')
        flag_id_list_all.append(flag_id_list)
    df_new['flag_id'] = flag_id_list_all
    
    flag_cell_list_all = []
    for i in range(len(df_new)):
        flag_cell_list = str(df_new['ApplyLoan_New_flag_cell'].iloc[i]).split('，')
        flag_cell_list_all.append(flag_cell_list)
    df_new['flag_cell'] = flag_cell_list_all
    
    result = pd.DataFrame(columns=['id','apply_date','api_code','flag_id','flag_cell'])
#    result.shape
    
#    df_new_1 = df_new.copy(deep=True)
    col_flag_cell = df_new['flag_cell'].iloc[0]
    col_flag_id = df_new['flag_id'].iloc[0]
    col_apply_date = df_new['apply_date'].iloc[0]
    col_api_code = df_new['api_code'].iloc[0]
    col_cus_num = [df_new['id'].iloc[0]] * len(df_new['flag_id'].iloc[0])
    for i in range(len(df_new)-1):
           col_flag_id.extend(df_new['flag_id'].iloc[i+1])
           col_apply_date.extend(df_new['apply_date'].iloc[i+1])
           col_api_code.extend(df_new['api_code'].iloc[i+1])
           col_flag_cell.extend(df_new['flag_cell'].iloc[i+1])
           col_cus_num.extend([df_new['id'].iloc[i+1]]*len(df_new['flag_id'].iloc[i+1]))
    result['id'] = col_cus_num
    result['apply_date'] = col_apply_date
    result['api_code'] = col_api_code
    result['flag_id'] = col_flag_id
    result['flag_cell'] = col_flag_cell
#    result.shape
    return result

def log_to_df_v3(data):
    df_new = data.copy(deep=True)
    
    result = pd.DataFrame()
    col_apply_date = []
    col_date = []
    col_api_code = []
    col_flag_id = []
    col_flag_cell = []
    col_cus_num = []
    
    ## 格式错误日志删除
    col_wrong_index = []
    
    for i in range(len(df_new)):
        apply_date_list = str(df_new['applyLoan_v3_rs'].iloc[i]).split('，')
        api_code_list = str(df_new['applyLoan_v3_apicode'].iloc[i]).split('，')
        flag_id_list = str(df_new['applyLoan_v3_flag_id'].iloc[i]).split('，')
        flag_cell_list = str(df_new['applyLoan_v3_flag_cell'].iloc[i]).split('，')

        if (len(apply_date_list) != len(flag_id_list)) | (len(api_code_list) != len(flag_id_list)):
            col_wrong_index.append(i)
            continue
        
        col_apply_date.extend(apply_date_list)
        col_api_code.extend(api_code_list)
        col_flag_id.extend(flag_id_list)
        col_flag_cell.extend(flag_cell_list)
        col_cus_num.extend([df_new['id'].iloc[i]]*len(apply_date_list))
        col_date.extend([df_new['user_date'].iloc[i]]*len(apply_date_list))
        
    result['id']  = col_cus_num
    result['apply_date']  = col_apply_date
    result['user_date']  = col_date
    result['api_code']  = col_api_code
    result['flag_id']  = col_flag_id
    result['flag_cell']  = col_flag_cell

    print('删除日志错误记录：',len(col_wrong_index),' 条')
    
    return result,col_wrong_index

def logdf_add_cid(result,cid_path):
#     print("1")
#     print(os.getcwd())
    type_label = pd.read_excel(cid_path)
    type_label.columns = ["cid","api_code"]
    type_label['api_code'] = [str(i) for i in type_label["api_code"]]
    result_cid_type = pd.merge(result,type_label,on='api_code',how='left')
    #result_cid_type['cid'] = result_cid_type['cid'].map(int)
    result_cid_type = result_cid_type.fillna({'cid':'其他','api_code':'其他'})
    return result_cid_type